<html>
<head>
<script>

//Plugin lifetime variables - state
CMemonaut_init();

//Setting up Chrome Extension framework handlers
chrome.extension.onConnect.addListener(CMemonaut_messageHandler);
chrome.tabs.onRemoved.addListener(CMemonaut_resetPlugin);
chrome.browserAction.onClicked.addListener(CMemonaut_defaultNotebook);

//////////////////////
//Function Definitions
//////////////////////

function CMemonaut_init(){
	CMemonaut_defaultNotebook.view = null;
	CMemonaut_defaultNotebook.tab = null;
	CMemonaut_postMemo.notification = null;
	CMemonaut_postMemo.notificationTimeout = null;
}

// Allowing only one memonaut tab to be open through the plugin
function CMemonaut_defaultNotebook(tab){

	if(!CMemonaut_defaultNotebook.tab) {
		CMemonaut_openNewTab();
		return;
	}

	CMemonaut_getHandle();
	CMemonaut_defaultAction(tab);
}

function CMemonaut_defaultAction(tab){
	if(CMemonaut_isCopyAllowed(tab)){
		chrome.tabs.executeScript(null, {file: "chrome/content_script.js"});
	}
	else{
		CMemonaut_showTab();
	}
}

function CMemonaut_messageHandler(port){
	if (port.name != 'memonaut')
		return;

	port.onMessage.addListener(function(data) {
		if (data.command == "postMemo") {
			CMemonaut_postMemo(data.selection);
		}

		if (data.command == "show") {
			CMemonaut_showTab();
		}
	});
}

function CMemonaut_postMemo(selection){
	if(CMemonaut_defaultNotebook.view){
		//TODO: add BR's for newlines for selection
		CMemonaut_defaultNotebook.view.addNoteToTop(selection); //TODO: OPTION: Top, Bottom, AfterCurrent
		CMemonaut_showNotification(selection);
	}
}

function CMemonaut_showNotification(bodytext){
	if(CMemonaut_postMemo.notification)
		CMemonaut_hideNotification();

	CMemonaut_postMemo.notification = webkitNotifications.createNotification(
	  'notification.png',  // icon url - can be relative
	  'Copied!',  // notification title
	  bodytext  // notification body text
	);

	CMemonaut_postMemo.notification.show();
	CMemonaut_postMemo.notificationTimeout = setTimeout( "CMemonaut_hideNotification()", 5000);
	//TODO: OPTION: Timeout length And Window-Switch
}

function CMemonaut_hideNotification(){
	clearTimeout(CMemonaut_postMemo.notificationTimeout);
	CMemonaut_postMemo.notification.cancel();
	CMemonaut_postMemo.notification = null;
	CMemonaut_postMemo.notificationTimeout = null;
}

function CMemonaut_isCopyAllowed(tab){
	if(tab.url.indexOf("http://")!= -1)  return true;
	if(tab.url.indexOf("https://")!= -1) return true;
	return false;
}

function CMemonaut_getHandle(){
	if(CMemonaut_defaultNotebook.view)
		return;

	var views = chrome.extension.getViews();
	for(var i=0; i<views.length; i++){
		var view = views[i];
		if(!view.initMemonaut) //Checking whether a "magic" function is present in the view
			continue;

		CMemonaut_defaultNotebook.view = view;
		view.onUnload = CMemonaut_resetPlugin;
	}
}

function CMemonaut_openNewTab(){
	chrome.tabs.create({url: "notebooks/memonaut.html", selected:false}, CMemonaut_registerTab);
}

function CMemonaut_registerTab(tab){
	CMemonaut_defaultNotebook.tab = tab;
}

function CMemonaut_showTab(tab){
	if(!CMemonaut_defaultNotebook.tab) return;

	chrome.tabs.update(CMemonaut_defaultNotebook.tab.id, {selected: true});
}

function CMemonaut_resetPlugin(tabID){
	//TODO: Save innerHTML in localStorage
	if(tabID == CMemonaut_defaultNotebook.tab.id)
		CMemonaut_init();
}

</script>
</head>
</html>
